import os
import json
import datetime

'''
2018-11-08
功能：
    向本地写入已签到的人的名字和签到时间
参数：
    name 签到人员
    time 签到时间
返回值：
    签到次数
'''
def write(name, time):
    if os.path.exists(getTime(time,'date')+'signed.json'):
        signed = json.load(open(getTime(time,'date')+'signed.json', 'r'))
        #print(signed)
        if name in signed:
            times = signed[name]
            '''
            tiem_obj = {'签到时间': getTime(time,'')}
            times.append(tiem_obj)
            signed[name] = times
            json.dump(signed, open(getTime(time, 'date')+'signed.json', 'w'))
            '''
            return len(times)
        else:
            times = []
            time_obj = {'签到时间': getTime(time, '')}
            times.append(time_obj)
            signed[name] = times
            json.dump(signed, open(getTime(time, 'date')+'signed.json', 'w'))
            return 0
    else:
        signed= {}
        times = []
        time_obj = {'签到时间': getTime(time, '')}
        times.append(time_obj)
        signed[name] = times
        json.dump(signed, open(getTime(time, 'date')+'signed.json', 'w'))
        return 0

'''
功能：
    返回指定一天的签到名单
参数：
    time 时间
返回值：
    签到名单（字典）
'''
def getList(time):
    try:
        signed = json.load(open(getTime(time, 'date')+'signed.json', 'r'))
        for name in signed:
            print(name+':', signed[name][0]['签到时间'])
    except:
        print('No record.')

'''
功能：
    转换时间格式
参数：
    time 时间
    format 格式参数，等于'date'时返回日期
返回值：
    将time转成str
'''
def getTime(time, format):
    if format == 'date':
        return str(time.year)+'-'+str(time.month)+'-'+str(time.day)
    else:
        return str(time.year)+'-'+str(time.month)+'-'+str(time.day)+' '+str(time.hour)+':'+str(time.minute)+':'+str(time.second)

if __name__ == '__main__':
    while True:
        t = input('The date to qury(YYYY-MM-DD): ')
        if t == 'exit':
            break
        tY, tM, tD = t.split('-')
        t = datetime.datetime(int(tY), int(tM), int(tD))
        getList(t)
